//
//  UIViewController+Base.m
//  PersonalTools
//
//  Created by Samuel on 2/9/15.
//  Copyright (c) 2015 Samuel. All rights reserved.
//

#import "UIViewController+Base.h"

@implementation UIViewController (Base)

//自定义natigationitem标题
-(void)setTitle:(NSString *)title
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    customLab.backgroundColor=[UIColor clearColor];
    [customLab setTextColor:[UIColor blackColor]];
    customLab.textAlignment=NSTextAlignmentCenter;
    customLab.numberOfLines = 2;
    [customLab setText:title];
    self.navigationItem.titleView=customLab;
}
-(UIControl*)setMainTitle:(NSString *)mTitle andTitle:(NSString*)title
{
    UILabel *mLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    mLab.backgroundColor=[UIColor clearColor];
    [mLab setTextColor:kUIColorFromRGB(0x007AFF)];
    mLab.textAlignment=NSTextAlignmentCenter;
    [mLab setFont:[UIFont boldSystemFontOfSize:14]];
    [mLab setText:mTitle];
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 120, 14)];
    customLab.backgroundColor=[UIColor clearColor];
    [customLab setTextColor:[UIColor lightGrayColor]];
    customLab.textAlignment=NSTextAlignmentCenter;
    [customLab setText:title];
    [customLab setFont:[UIFont systemFontOfSize:10]];
    
    UIControl *views = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, 120, 44)];
    [views setBackgroundColor:[UIColor clearColor]];
    [views addSubview:mLab];
    [views addSubview:customLab];
    
//    UIButton *buttonAction = [UIButton buttonWithType:UIButtonTypeCustom];
//    [buttonAction setBackgroundColor:[UIColor clearColor]];
//    [buttonAction setFrame:views.frame];
//    [buttonAction setCenter:views.center];
//    [views addSubview:buttonAction];
    
    self.navigationItem.titleView=views;
    return views;
}

- (UIButton*)setDefaultLeftButton
{
    UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 44)];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    [leftButton setBackgroundColor:[UIColor greenColor]];
    return leftButton;
}
- (UIButton*)setDefaultRightButton
{
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 44)];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    [rightButton setTitleColor:kUIColorFromRGB(0x007AFF) forState:UIControlStateNormal];
    [rightButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    return rightButton;
}


- (UIButton*)setLeftButtonWithBack;
{
    UIButton *button = [self setDefaultLeftButton];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchDown];
    return button;
}

- (void)back:(UIButton*)sender
{
//    if (![self isMemberOfClass:[self class]]) {
        if (self.parentViewController.childViewControllers.count>1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            if (self.presentingViewController) {
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }
        }
//    }
}
@end
